<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Control extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'));
        $this->data['controller'] = 'control';
        $this->load->model('Mcontrol');
    }
    
	public function addquestion()
    {
        $tvshow_id = $this->getParamInt('tvshow_id');
        $this->load->library('form_validation');
		$this->form_validation->set_rules('question', 'Nội dung câu hỏi', 'required');
		$this->form_validation->set_rules('choice-1', 'Phương án 1', 'required');
		$this->form_validation->set_rules('choice-2', 'Phương án 2', 'required');
		$this->form_validation->set_rules('choice-3', 'Phương án 3', 'required');
		$this->form_validation->set_rules('choice-4', 'Phương án 4', 'required');
        
		$this->data['data'] = '';
        $this->data['list_tvshow']  = $this->Mcontrol->getTvshowCategories();
		$this->data['get_tvshow'] = $this->Mcontrol->ajaxGetTvshow($tvshow_id);
        $this->data['infoStart'] = $this->Mcontrol->getIDChannelTemplate($tvshow_id);
        $this->data['data_vote'] = $this->Mcontrol->getVoteListTvShow($tvshow_id);
        $this->data['data_vote_is_start'] = $this->Mcontrol->getVoteListTvShowIsStart($tvshow_id);
        
        
        
        if($this->form_validation->run() === TRUE)
        {
			
            $question = $this->getParamString('question');
            $choice1 = $this->getParamString('choice-1');
            $choice2 = $this->getParamString('choice-2');
            $choice3 = $this->getParamString('choice-3');
            $choice4 = $this->getParamString('choice-4');
			
            $this->data['data'] = $this->Mcontrol->addThisQuestion($tvshow_id,$question,$choice1,$choice2,$choice3,$choice4);
            if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm câu hỏi thành công!");window.location.href="'.$this->data['baseurl'].'manage/control/add-question/";</script>');
            }
        }
        
        $this->data['action'] = 'addquestion';
        $this->load->view('template/layout',$this->data);
    }
    
    
    public function ajaxFilterTvshow()
    {
        $keyword = $this->getParamString("keyword");
        $this->data['data']  = $this->Mcontrol->ajaxFilterTvshow($keyword);
         die(json_encode($this->data['data']));
        $this->data['action'] = 'ajaxFilterTvshow';
        $this->load->view('template/control/ajaxFilterTvshow',$this->data);
    }
    
    public function ajaxGetTvshow()
    {
        $tvshow_id = $this->getParamInt("tvshow_id");
        $this->data['data']  = $this->Mcontrol->ajaxGetTvshow($tvshow_id);
        die(json_encode($this->data['data']));
        $this->data['action'] = 'ajaxGetTvshow';
        $this->load->view('template/control/ajaxGetTvshow',$this->data);
    }
    
    public function updatetvshow()
    {
        $tvshow_id = $this->getParamInt("tvshow_id");
        $tvshow_name = $this->getParamString("tvshow_name");
        $channel_id = $this->getParamInt("channel_id");
        die($this->Mcontrol->UpdateTvShowDB($tvshow_id,$tvshow_name,$channel_id));
    }
    public function stoptvshow()
    {
        $id_log = $this->getParamInt("id_log");
        die($this->Mcontrol->StopTvShowDB($id_log));
    }
    
    public function updatestartquestion()
    {
        $id_question = $this->getParamInt("id_question");
        die($this->Mcontrol->UpdateStatusQuestionDB($id_question, 1));
    }
    
    public function updatestopquestion()
    {
        $id_question = $this->getParamInt("id_question");
        die($this->Mcontrol->UpdateStatusQuestionDB($id_question, 2));
    }
    
    public function questionHistories()
    {
        $id = $this->getParamInt('id');
        $this->data['data'] = $this->Mcontrol->questionHistoriesDb($id);
        $this->data['action'] = 'questionhistory';
        $this->load->view('template/layout', $this->data);
    }
    public function showHistories()
    {
        $this->data['data'] = $this->Mcontrol->showHistoriesDb();
        $this->data['action'] = 'showhistory';
        $this->load->view('template/layout', $this->data);
    }	
}
